<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author Gobmos Lorand
 */
class MY_Model extends CI_Model {
    public $fields = array();
    /**
     * Constructor
     *
     * @access public
     * @param string $class_name
     * @param string $table_name
     */
	function __construct(){
		parent::__construct();
	}
	
    function init($class_name = '', $table_name = '') {
    	$this->class_name = $class_name;
        $tables = $this->config->item('tables');
        $this->fields = $tables[$table_name];
		/*
        foreach ($this->fields as $key => $val) {
            //$this->$key = $val['default'];
			$this->$key = '';
        }*/
    }
    /**
     * Generate array from class atributes
     *
     * @access public
     * @return mixed
     */
    public function fields_to_object() {
        $obj = new $this->class_name();
        foreach ($this->fields as $key => $val) {
        	if (!is_array($val)) {
				$obj->$key = $val;	
        	}  
        }

        return $obj;
    }
        
    /**
     * Generate array from class atributes
     *
     * @access public
     * @return mixed
     */
    public function to_array() {
        $array = array();
        foreach ($this->fields as $key => $val) {
            if ( isset($this->$key) ) {
                $array[$key] = $this->$key;
            }

        }
        return $array;
    }
    
    /**
     * 
     * Clones the current object
     */
    public function cloneMe() {
    	$clone = $this;
    	foreach ($clone->fields as $key => $value) {
    		if (isset($clone->$key)) {
    			if ($key != 'id' && $key !='language_id' && $key != 'imported') {
    				$clone->fields[$key] = $clone->$key;
    			}
    			else {
    				$clone->$key = 0;
    			}
    		}
    	}
    	return $clone;
    }
    /**
     * Generate oject from array
     *
     * @access public
     * @param mixed $array
     * @return object
     */
    public function from_array($array) {   	
        $obj = new $this->class_name();
        if(!is_array($array)) return false;
        foreach ($array as $key => $val) {
            $obj->$key = $val;
        }
        return $obj;
    }
    /**
     * Get row from DB given by id
     *
     * @access public
     * @param integer $id
     * @return object
     */
    public function get_by_id($id) {
        $result =  $this->db->select()->from($this->table)->where('id',$id)->get()->row();
        $obj = FALSE;
        if ($result) {
            $obj = $this->from_array((array)$result);
        }
        return $obj;
    }
    
    /**
     * Get row from DB given by param
     *
     * @access public
     * @param array $param for i.e: array('table_field'=> 'value');
     * @return array
     */
    public function get_by_param($param, $order = array()) {
    	if (!empty($order)){
    		return $this->db->select()->from($this->table)->where($param)->order_by($order['index'], $order['sort_order'])->get()->result();
    	}
    	return $this->db->select()->from($this->table)->where($param)->get()->result();
    }
    
    public function getAll() {
    	return $this->db->select()->from($this->table)->get()->result();	
    }
	
    public function countAll() {
    	return $this->db->select()->from($this->table)->get()->num_rows();	
    }	
    /**
     * Insert data in DB
     *
     * @access public
     * @return integer
     */
    public function insert() {
        $data = $this->to_array();
        $this->db->insert($this->table,$data);
        return $this->db->insert_id();
    }
    /**
     * Update data in DB
     *
     * @access public
     * @return bool
     */
    public function update() {
    	
        $data = $this->to_array();
        $this->db->where('id',$this->id);
        $this->db->update($this->table,$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    
    /**
     * Update data in DB
     *
     * @access public
     * @param array $data
     * @return bool
     */
    public function updateExternData($data, $id = 0) {
    	if (empty($data)) {
    		return false;
    	}
    	if ($id) {
        	$this->db->where('id',(int) $id);
    	}
        $this->db->update($this->table,$data);

        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }    
    /**
     * Delete data from DB given by id
     *
     * @access public
     * @return bool
     */
    public function delete($where = '') {
    	$where = (empty($where)) ? 'id = ' . $this->id : $where;
        $this->db->where($where);
        $this->db->delete($this->table);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
?>
